#角色：接口自动化测试工程师
#任务：用python+requests完成登录接口测试
#接口说明：
# - 系统基本路径：http://kdtx-test.itheima.net
# ## 登录
# ### 生成验证码
# PATH:/api/captchaImage
# Type: GET
# Description:生成验证码
# Response-example:
# 响应状态码：200
# 响应数据：`{ "msg": "操作成功", "img": "......", "code": 200, "uuid": "xxxxxx"}`
import requests

# 定义系统基本路径
base_url = "http://kdtx-test.itheima.net"

# 请求生成验证码的接口路径
captcha_path = "/api/captchaImage"
captcha_url = base_url + captcha_path

# 发送GET请求
response = requests.get(captcha_url)

# 检查响应状态码是否为200
if response.status_code == 200:
    print("请求成功！")
    data = response.json()

    # 打印关键字段
    print(f"msg: {data.get('msg')}")
    print(f"code: {data.get('code')}")
    print(f"uuid: {data.get('uuid')}")

    # 可选：保存验证码图片
    img_data = data.get('img')
    if img_data:
        with open("captcha_image.png", "wb") as f:
            import base64
            f.write(base64.b64decode(img_data))
        print("验证码图片已保存为 captcha_image.png")
else:
    print(f"请求失败，状态码：{response.status_code}")
#  登录
import json

url = "http://kdtx-test.itheima.net/api/login"

payload = json.dumps({
   "username": "admin",
   "password": "HM_2023_test",
   "code": "2",
   "uuid": "686e82a375e44cd4b83eaad0bd2833cb"
})
headers = {
   'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
